﻿using MVC_No07.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MVC_No07.Controllers
{
    public class MyHomeController : Controller
    {
        [HttpGet]
        public ActionResult Index(string UidList = null, string Text = null)
        {
            List<TaskManagement> list;

            using (Model1 model = new Model1())
            {
                list = model.TaskManagement.Where(u => true).ToList();
                if (!string.IsNullOrEmpty(UidList) && int.TryParse(UidList, out int i))
                {
                    int id = Convert.ToInt32(UidList);
                    if (id != 0)
                        list = list.Where(u => u.uid == id).ToList();
                }
                if (!string.IsNullOrEmpty(Text))
                {
                    list = list.Where(u => u.Content.IndexOf(Text) >= 0).ToList();
                }
            }

            ViewBag.UidList = GetUid();

            Dictionary<int, string> userInfoD = new Dictionary<int, string>();
            foreach (var item in GetUserInfoList())
                userInfoD.Add(item.uid, item.name);
            ViewBag.userInfoList = userInfoD;//获取用户信息集合

            return View(list);
        }

        [HttpGet]
        public ActionResult Create()
        {
            ViewBag.TaskPriorityList = GetTaskPriorityList(true);
            ViewBag.UidList = GetUid(true);
            return View(new TaskManagement());
        }

        [HttpPost]
        public ActionResult Create(TaskManagement taskManagement)
        {
            taskManagement.AddTime = DateTime.Now;
            taskManagement.TaskState = 2;
            using (Model1 model = new Model1())
            {
                model.TaskManagement.Add(taskManagement);
                if (model.SaveChanges() == 1)
                    return RedirectToAction("Index", "MyHome");
                else
                    return RedirectToAction("Create", "MyHome");
            }
        }

        [HttpPost]
        public ActionResult Delete(string uid)
        {
            if (!int.TryParse(uid, out int i))
                return Json(2);
            var id = Convert.ToInt32(uid);
            using (Model1 model = new Model1())
            {
                model.TaskManagement.Remove(model.TaskManagement.FirstOrDefault(u => u.TaskID == id));
                return Json(model.SaveChanges());
            }
        }

        [HttpGet]
        public ActionResult Update(string uid)
        {
            if (!int.TryParse(uid, out int i))
                return RedirectToAction("Index", "MyHome");
            var id = Convert.ToInt32(uid);
            using (Model1 model = new Model1())
            {
                var task = model.TaskManagement.FirstOrDefault(u => u.TaskID == id);

                ViewBag.AUidList = GetUid(Convert.ToInt32(task.uid) - 1);
                ViewBag.TaskPriorityList = GetTaskPriorityList(Convert.ToInt32(task.TaskPriority) - 1);
                ViewBag.TaskStateList = GetTaskStateList(Convert.ToInt32(task.TaskState) - 1);

                return View(task);
            }
        }

        [HttpPost]
        public ActionResult Update(TaskManagement taskManagement)
        {
            using (Model1 model = new Model1())
            {
                var taskStateOld = model.TaskManagement.FirstOrDefault(u => u.TaskID == taskManagement.TaskID).TaskState;
                var taskStateNew = taskManagement.TaskState;

                var info = model.TaskManagement.FirstOrDefault(u => u.TaskID == taskManagement.TaskID);

                info.TaskPriority = taskManagement.TaskPriority;
                info.Content = taskManagement.Content;

                if (taskStateOld == 2 && taskStateNew == 1)
                {
                    info.FinishedTime = DateTime.Now;
                    info.TaskState = 1;
                }

                info.uid = taskManagement.uid;

                if (model.SaveChanges() == 1)
                    return RedirectToAction("Index", "MyHome");
                else
                    return RedirectToAction("Index", "MyHome");
            }
        }



        private List<UserInfo> GetUserInfoList()//获取用户信息集合
        {
            List<UserInfo> list = null;
            using (Model1 model = new Model1())
            {
                list = model.UserInfo.Where(u => true).ToList();
            }
            return list;
        }

        private SelectList GetUid()//UidList
        {
            var list = GetUserInfoList();
            var listItems = new List<SelectListItem>();
            listItems.Add(new SelectListItem() { Text = "全部", Value = "0", Selected = true });
            foreach (var item in list)
                listItems.Add(new SelectListItem() { Text = item.name, Value = item.uid.ToString(), Selected = false });
            return new SelectList(listItems, "Value", "Text", "Selected");
        }

        private SelectList GetUid(bool defaultMy)//UidList
        {
            var list = GetUserInfoList();
            var listItems = new List<SelectListItem>();
            foreach (var item in list)
                listItems.Add(new SelectListItem() { Text = item.name, Value = item.uid.ToString(), Selected = false });

            listItems[0].Selected = defaultMy;
            return new SelectList(listItems, "Value", "Text", "Selected");
        }

        private SelectList GetUid(int index)//UidList
        {
            var listItems = new List<SelectListItem>();
            foreach (var item in GetUserInfoList())
                listItems.Add(new SelectListItem() { Text = item.name, Value = item.uid.ToString(), Selected = false });

            listItems[index].Selected = true;
            return new SelectList(listItems, "Value", "Text", "Selected");
        }

        private SelectList GetTaskPriorityList(bool defaultMy)//TaskPriorityList
        {
            return new SelectList(new List<SelectListItem>() {
                new SelectListItem(){ Text = "高" , Value = "1", Selected = defaultMy},
                new SelectListItem(){ Text = "中" , Value = "2", Selected = false},
                new SelectListItem(){ Text = "低" , Value = "3", Selected = false},
            }, "Value", "Text", "Selected");
        }

        private SelectList GetTaskPriorityList(int index)//TaskPriorityList
        {
            var list = new List<SelectListItem>() {
                new SelectListItem(){ Text = "高" , Value = "1", Selected = false},
                new SelectListItem(){ Text = "中" , Value = "2", Selected = false},
                new SelectListItem(){ Text = "低" , Value = "3", Selected = false},
            };
            list[index].Selected = true;
            return new SelectList(list, "Value", "Text", "Selected");
        }

        private SelectList GetTaskStateList(int index)
        {
            var list = new List<SelectListItem>() {
                new SelectListItem(){ Text = "已完成" , Value = "1", Selected = false},
                new SelectListItem(){ Text = "未完成" , Value = "2", Selected = false},
            };
            list[index].Selected = true;
            return new SelectList(list, "Value", "Text", "Selected");
        }
    }
}